package imanager.showcases.agenda.client;

import imanager.showcases.agenda.entity.Anexo;
import imanager.showcases.agenda.entity.TipoPessoa;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import com.googlecode.imanager.client.Control;
import com.googlecode.imanager.client.Document;
import com.googlecode.imanager.client.Label;
import com.googlecode.imanager.client.Panel;
import com.googlecode.imanager.client.Picture;
import com.googlecode.imanager.client.action.Action;
import com.googlecode.imanager.client.action.DisplayAction;
import com.googlecode.imanager.client.form.Button;
import com.googlecode.imanager.client.form.CheckBox;
import com.googlecode.imanager.client.form.ComboBox;
import com.googlecode.imanager.client.form.ListBox;
import com.googlecode.imanager.client.form.RadioButton;
import com.googlecode.imanager.client.form.TextArea;
import com.googlecode.imanager.client.form.TextBox;
import com.googlecode.imanager.client.io.Path;
import com.googlecode.imanager.client.layout.Overflow;
import com.googlecode.imanager.client.layout.ScrollBox;
import com.googlecode.imanager.client.util.Anchors;
import com.googlecode.imanager.client.value.format.Formatter;
import com.googlecode.imanager.client.value.mask.SimpleMask;

/**
 * @author wellsantos@wellsantos.com
 * @since 0.01
 */
public class CadastroEntidadeDocument extends Document {

    private static final long serialVersionUID = 1L;   
    
    protected ScrollBox detailPanel;
    protected Panel footerPanel;

    protected Label nomeLabel;
    protected TextBox nomeTextBox;
    
    protected Label pessoaLabel;
    protected ComboBox<TipoPessoa> pessoaComboBox;
    
    protected Label fotoLabel;
    protected ListBox<Anexo> fotoListBox;
    protected Picture fotoPicture;
    
    protected Label nascimentoLabel;
    protected TextBox nascimentoTextBox;
    
    protected Label observacaoLabel;
    protected TextArea observacaoTextArea;
    
    protected CheckBox checkBox;
    protected RadioButton radioButton;

    protected Button buttonSalvar;
    protected Button buttonCancelar;
    
    
    public CadastroEntidadeDocument() {
        setOverflow(Overflow.HIDDEN, Overflow.HIDDEN);
        setSize(400, 400);
        initializeControls();
        setTitle("Cadastro de Entidade");
        //inputFoto.setSelectedIndex(1);        
    }

    protected void initializeControls() {
        detailPanel = new ScrollBox(this);
        footerPanel = new Panel(this);
        nomeLabel = new Label(detailPanel);
        nomeTextBox = new TextBox(detailPanel);
        pessoaLabel = new Label(detailPanel);
        pessoaComboBox = new ComboBox<TipoPessoa>(detailPanel); 
        fotoLabel = new Label(detailPanel);
        fotoListBox = new ListBox<Anexo>(detailPanel);
        fotoPicture = new Picture(detailPanel);
        nascimentoLabel = new Label(detailPanel);
        nascimentoTextBox = new TextBox(detailPanel);
        observacaoLabel = new Label(detailPanel);
        observacaoTextArea = new TextArea(detailPanel);      
        checkBox = new CheckBox(detailPanel);
        radioButton = new RadioButton(detailPanel);
        buttonSalvar = new Button(footerPanel);
        buttonCancelar = new Button(footerPanel);

        detailPanel.setAnchors(Anchors.STRETCH);
        detailPanel.setPosition(0, 0, 0, 40);

        footerPanel.setAnchors(Anchors.HORIZONTAL_BOTTOM);
        footerPanel.setPosition(0, null, 0, 0);
        footerPanel.setHeight(40);

        nomeLabel.setText("Nome");
        nomeLabel.setBounds(20, 10, 100, 20);
        nomeLabel.setTarget(nomeTextBox);
        
        nomeTextBox.setAnchors(Anchors.HORIZONTAL_TOP);
        nomeTextBox.setPosition(110, 10, 20, null);
        nomeTextBox.setAutoTrim(true);

        pessoaLabel.setText("Pessoa");
        pessoaLabel.setBounds(20, 40, 100, 20);
        pessoaLabel.setTarget(pessoaComboBox);
        
        pessoaComboBox.setAnchors(Anchors.HORIZONTAL_TOP);
        pessoaComboBox.setBounds(110, 40, 140, 20);
        pessoaComboBox.setItems(Arrays.asList(TipoPessoa.values()));
        pessoaComboBox.setAction(new Action(){
            private static final long serialVersionUID = 1L;

            @Override
            protected void doExecute(Control sender) {
                if (pessoaComboBox.getSelectedItem() == TipoPessoa.JURIDICA){
                    fotoLabel.setText("Logotipo Firma");
                    nascimentoLabel.setText("Fundado em");
                } else {
                    fotoLabel.setText("Fotografia");
                    nascimentoLabel.setText("Data Nasc.");
                }
            }
        });

        fotoLabel.setText("Fotografia");
        fotoLabel.setBounds(20, 70, 100, 20);
        fotoLabel.setTarget(fotoListBox);

        fotoListBox.setAnchors(Anchors.HORIZONTAL_TOP);
        fotoListBox.setBounds(110, 70, 140, 80);
        fotoListBox.setItems(getListaAnexos());
        fotoListBox.setFormatter(new Formatter<Anexo>() {
            private static final long serialVersionUID = 1L;

            @Override
            public String format(Anexo value) {
                return value.getTitle();
            }
        });
        fotoListBox.setAction(new Action() {
            private static final long serialVersionUID = 1L;

            @Override
            protected void doExecute(Control sender) {
                Anexo anexo = fotoListBox.getSelectedItem();
                if (anexo != null){
                    Path path = new Path(anexo.getUri());
                    fotoPicture.setImage(loadImage(path));
                } else {
                    fotoPicture.setImage(null);
                }
            }
        });
        
        fotoPicture.setAnchors(Anchors.TOP_RIGHT);
        fotoPicture.setBounds(260, 40, 120, 110);
        
        nascimentoLabel.setText("Data Nasc.");
        nascimentoLabel.setBounds(20, 160, 100, 20);
        
        nascimentoTextBox.setBounds(110, 160, 100, 20);
        nascimentoTextBox.setMask(new SimpleMask("99/99/9999"));
        
        observacaoLabel.setText("Observações");
        observacaoLabel.setBounds(20, 190, 100, 20);
        observacaoLabel.setTarget(observacaoTextArea);
        
        observacaoTextArea.setBounds(110, 190, 270, 60);
        
        checkBox.setBounds(110, 260, 150, 20);
        checkBox.setText("Um check box.");

        radioButton.setBounds(110, 280, 150, 20);
        radioButton.setText("Um radio button.");
        
        buttonSalvar.setAnchors(Anchors.RIGHT_BOTTOM);
        buttonSalvar.setRightBottom(110, 10);
        buttonSalvar.setSize(80, 20);
        buttonSalvar.setText("Salvar");
        buttonSalvar.setAction(new Action() {
            private static final long serialVersionUID = 1L;

            @Override
            protected void doExecute(Control sender) {
                getApplication().showMessageDialog(getTitle(), ""
                        + "Nome=" + nomeTextBox.getText() + "; "
                        + "Pessoa=" + pessoaComboBox.getSelectedItem() + "; "
                        + "Foto=" + fotoListBox.getSelectedText() + "; "
                        + "Observ=" + observacaoTextArea.getText() + "; "
                        + "CheckBox=" + checkBox.getChecked() + "; "
                        + "RadioBtn=" + radioButton.getChecked() + "; "
                );
                nomeTextBox.focus();
            }
        });

        buttonCancelar.setAnchors(Anchors.RIGHT_BOTTOM);
        buttonCancelar.setRightBottom(10, 10);
        buttonCancelar.setSize(90, 20);
        buttonCancelar.setText("Cancelar");
        buttonCancelar.setAction(new DisplayAction(this, false));
    }

    private List<Anexo> getListaAnexos() {
        List<Anexo> anexos = new ArrayList<Anexo>();
        for (int i=1; i<=10; i++){
            anexos.add(new Anexo("Foto " + i, "images/foto"+i+".jpg"));
        }
        return anexos;
    }

    
}
